package middleware

import (
    "fmt"
    "github.com/afex/hystrix-go/hystrix"
    "github.com/gin-gonic/gin"
)

func NewHystrix() gin.HandlerFunc {
    config := hystrix.CommandConfig{
        Timeout:                5000,
        MaxConcurrentRequests:  100,
        SleepWindow:            5000, //过多长时间熔断器再次检测是否开启
        RequestVolumeThreshold: 20,   //有多少个请求才进行错误百分比计算
        ErrorPercentThreshold:  25,   //发生熔断比例
    }
    hystrix.ConfigureCommand("default", config)
    return func(c *gin.Context) {
        hystrix.Do("default", func() error {
            c.Next()
            return nil
        }, func(err error) error {
            fmt.Println("触发了熔断器规则")
            c.Abort()
            return err
        })
    }
}
